package org.friping.mytool.views;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.widget.TextView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;
import org.friping.mytool.R;
import org.friping.mytool.dao.UnitBudgetDao;
import org.friping.mytool.entity.UnitBudget;
import org.friping.mytool.helper.ActivityHelper;
import org.friping.mytool.helper.Constants;
import org.friping.mytool.helper.UITypeHelper;
import org.friping.mytool.views.custom.adapter.DeleteAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EActivity(R.layout.a_unit_budget_list)
public class UnitBudgetListActivity extends AppCompatActivity {

    @ViewById
    Toolbar toolbar;

    @ViewById
    ListView listView;

    @ViewById
    TextView btnEdit;

    @Bean
    UITypeHelper uiTypeHelper;

    @Bean
    ActivityHelper activityHelper;

    @Bean
    DeleteAdapter listViewAdapter;

    @Bean
    UnitBudgetDao unitBudgetDao;

    Integer selectPos = -1;

    @Extra
    Long unitId;

    @AfterViews
    public void afterView(){
        setSupportActionBar(toolbar);
        initListView();
    }

    @Click(R.id.btnEdit)
    public void toggleEdit(){
        listViewAdapter.setShowDelete(!listViewAdapter.isShowDelete());
        btnEdit.setText(listViewAdapter.isShowDelete() ? R.string.back : R.string.edit);
    }

    @ItemClick
    public void listViewItemClicked(int position){
        Map itemObj = (Map) listViewAdapter.getItem(position);
        Long id = (Long) itemObj.get("id");
        Intent intent = activityHelper.buildIntent(this, UnitBudgetEditActivity_.class);
        intent.putExtra("unitBudgetId", id);
        intent.putExtra("unitId", unitId);
        selectPos = position;
        this.startActivityForResult(intent, Constants.EDIT_UNIT_BUDGET_REQUEST_CODE);
    }

    @Click(R.id.addUnitBudgets)
    public void addUnitBudgets(){
        Intent intent = activityHelper.buildIntent(this, UnitBudgetEditActivity_.class);
        intent.putExtra("unitId", unitId);
        this.startActivityForResult(intent, Constants.EDIT_UNIT_BUDGET_REQUEST_CODE);
    }

    @OnActivityResult(Constants.EDIT_UNIT_BUDGET_REQUEST_CODE)
    void onResult(int resultCode, Intent data) {
        switch (resultCode) {
            case Constants.EDIT_UNIT_BUDGET_RESULT_CODE:
            if(selectPos > -1){
                Map item = (Map) listViewAdapter.getItem(selectPos);
                item.put("budget", data.getStringExtra("budget"));
                item.put("budgetDate", data.getStringExtra("budgetDate"));
                item.put("isSelf", data.getStringExtra("isSelf"));
                item.put("title", data.getStringExtra("budgetDate"));
                listViewAdapter.notifyDataSetChanged();
            }else{
                Map item = new HashMap();
                item.put("id", data.getLongExtra("id", -1));
                item.put("budget", data.getFloatExtra("budget", 0f));
                item.put("budgetDate", data.getStringExtra("budgetDate"));
                item.put("isSelf", data.getBooleanExtra("isSelf", false));
                item.put("title", data.getStringExtra("budgetDate"));
                listViewAdapter.addItem(item);
            }
        }
        selectPos = -1;
    }


    private void initListView() {
        List unitBudgets = unitBudgetDao.listWithMap(UnitBudget.class, "unitId=?", new String[] {String.valueOf(unitId)}, null, null, null, null);

        unitBudgets = wrapItemsData(unitBudgets);
        listViewAdapter.refresh((List<Map>) unitBudgets);
        listViewAdapter.setDeleteCallback(pos -> {
            Map itemData = (Map) listViewAdapter.getItem(pos);
            unitBudgetDao.delete(UnitBudget.class, (Long) itemData.get("id"));
        });
        listView.setAdapter(listViewAdapter);

    }

    private List<Map> wrapItemsData(List<Map> items){
        for(Map<String, Object> item : items){
            Object value = item.getOrDefault("budgetDate", null);
            item.put("title", uiTypeHelper.getDisplayValue(UITypeHelper.Type.Date, value));
        }
        return items;
    }
}
